package com.sduept.nwld.dataserver.rest;

import java.util.List;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sduept.bigdata.weather.map.MapDataCache;
import com.sduept.component.message.model.CimEquipmentUpdateMessage;
import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.manager.message.CimEquipmentUpdateMessageManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 风险预警REST服务
 */
@RestController
@RequestMapping("/cim/")
public class CimServices {

	@Autowired
	private SuperDAO dao;
	@Autowired
	private MapDataCache cache;
	@Autowired
	private CimEquipmentUpdateMessageManager cimUpdate;

	@GetMapping("/getStations/{volatge}")
	public String getStations(@PathVariable("volatge") int volatge) {
		//		4785074607226881
		JSONArray rtn = new JSONArray();
		if (volatge == 220) {
			rtn.addAll(cache.getS220List());

		} else if (volatge == 500) {
			rtn.addAll(cache.getS500List());

		} else if (volatge == 800) {
			rtn.addAll(cache.getS800List());

		}
		return rtn.toJSONString();
	}
	/**
	 * 获取断路器
	 * @return [[id,name]]
	 */

	@GetMapping("/getBreakersStationId/{sid}")
	public String getBreakersStationId(@PathVariable("sid") String sid) {
		String s1 = "select t.id,t.name from t_cim_breaker t where t.sub_id='" + sid + "' and standVoltagelevel > 499";
		List<?> breakers = dao.nativeQuery(s1);
		JSONArray rtn = new JSONArray();
		rtn.addAll(breakers);
		return rtn.toJSONString();
	}

	/**
	 * 获取变压器
	 * @return [[id,name]]
	 */
	@GetMapping("/getTransformByStationId/{sid}")
	public String getTransformByStationId(@PathVariable("sid") String sid) {
		String s1 = "select t.id,t.name from t_cim_powertransformer t where t.equipmentContainer='" + sid + "'";
		List<?> objs = dao.nativeQuery(s1);
		JSONArray rtn = new JSONArray();
		rtn.addAll(objs);
		return rtn.toJSONString();
	}

	/**
	 * 获取保护
	 * @return [{type:'',value:[ID,name,devType]}]
	 */
	@GetMapping("/getProtectsStationId/{sid}")
	public String getProtectsStationId(@PathVariable("sid") String sid) {
		JSONArray rtn = new JSONArray();
		String sqlStr = "select t.mRID,t.mProtectName,t.devType from t_bx_protection_equipment t where t.cim_station_id='"
				+ sid + "' and mPrimdevID like";
		String xlusql = sqlStr + " '%XL%'";
		String byqsql = sqlStr + " '%BYQ%'";
		String mxsql = sqlStr + " '%MX%'";
		String kgsql = sqlStr + " '%KG%'";
		List<?> byqs = dao.nativeQuery(byqsql);
		JSONObject byq = new JSONObject();
		byq.put("type", "变压器");
		byq.put("value", byqs);
		rtn.add(byq);
		List<?> xlus = dao.nativeQuery(xlusql);
		JSONObject xl = new JSONObject();
		xl.put("type", "线路");
		xl.put("value", xlus);
		rtn.add(xl);
		List<?> mxs = dao.nativeQuery(mxsql);
		JSONObject mx = new JSONObject();
		mx.put("type", "母线");
		mx.put("value", mxs);
		rtn.add(mx);
		List<?> kgs = dao.nativeQuery(kgsql);
		JSONObject kg = new JSONObject();
		kg.put("type", "开关");
		kg.put("value", kgs);
		rtn.add(kg);
		return rtn.toJSONString();
	}

	/**
	 * 查找最近3天的告警消息
	 * @param date
	 * @return
	 */

	@GetMapping("/findLast3DaysMessages")
	public String findLast3DaysMessages() {
		List<CimEquipmentUpdateMessage> messages = cimUpdate.findLast3DaysMessages(true);
		return com.alibaba.fastjson.JSONArray.toJSONString(messages);
	}

}
